package com.jjh.utils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {

    /**
     * md5加密
     * @param password
     * @return
     */
    public static String encode(String password){
        String encryptedPass = null;
        try{
            MessageDigest messageDigest = MessageDigest.getInstance("MD5");
            //将密码转为字节数组
            byte[] bytes = password.getBytes(StandardCharsets.UTF_8);
            //对密码进行摘要计算
            byte[] digest = messageDigest.digest(bytes);
            //将结果转为十六进制
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b & 0xff));
            }
            encryptedPass = sb.toString();

        }catch (NoSuchAlgorithmException e){
              throw new RuntimeException("md5加密出错!");
        }
         return encryptedPass;
    }





}
